﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MaLu.Data;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Text;
using MaLu.Framework.Common;

namespace MaluJg.web.PovertyIn
{
    public partial class USick : PUserControl
    {
        public void SaveData()
        {
            float f = 0.0f;
            float temp = 0.0f;
            float.TryParse(tbDistrict.Text, out temp);
            f += temp;
            temp = 0.0f;
            float.TryParse(tbTown.Text, out temp);
            f += temp;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("declare @id int; set @id={0}; select @id=id from SSick where id=@id;", RecordID);
            sb.AppendLine("if @@rowcount =0 begin");
            sb.AppendFormat(@"INSERT INTO [SSick]
           ([Name],[Gender],[Age],[Dept],[SickName],[IsTownFamliy],[IsVillageFamliy],[IsTownMedical]
           ,[IsVillageMedical],[AppliedMoney] ,[TownMoney],[DistrictMoney],[BelongTo] ,[Year] ,[Month],[Money])
     VALUES
           ({0},'{1}',{2},{3},{4},'{5}','{6}','{7}','{8}',{9},{10},{11},'{12}',{13},'{14}',{15}); end else begin

UPDATE [SSick]
   SET [Name] ={0}     ,[Gender] = '{1}'
      ,[Age] = {2}     ,[Dept] =  {3}
      ,[SickName] =  {4}      ,[IsTownFamliy] = '{5}'
      ,[IsVillageFamliy] =  '{6}'     ,[IsTownMedical] = '{7}'
      ,[IsVillageMedical] =  '{8}',[AppliedMoney] = {9}
      ,[TownMoney] = {10} ,[DistrictMoney] = {11}
      ,[BelongTo] = '{12}' ,[Year] =  {13},[Month] =  '{14}',[Money] =  {15} 
 WHERE id=@id ; end", Util.ReturnSqlValue(tbName.Text), ddlGendar.SelectedValue, Util.ReturnSqlValue(tbAge.Text), Util.ReturnSqlValue(tbDept.Text),
                    Util.ReturnSqlValue(tbSickName.Text),  (rblFamily.Items[0].Selected?"是":"否" ),
                    (rblFamily.Items[1].Selected ? "是" : "否"), (rblYiBao.Items[0].Selected ? "是" : "否"),
                     (rblYiBao.Items[1].Selected ? "是" : "否"), Util.ReturnSqlValue(tbAppliedMoney.Text),
                     Util.ReturnSqlValue(tbTown.Text),Util.ReturnSqlValue(tbDistrict.Text), ddlVillage.SelectedItem.Text,
                    Util.ReturnSqlValue(tbYear.Text), ddlMonth.SelectedValue, f  );

            SqlHelper.Instance.ExecuteNonQuery(Provider, sb.ToString());

        }
        public void LoadData()
        {
            ddlVillage.DataSource = BLPoverty.GetFType(Provider);
            ddlVillage.DataTextField = "name";
            ddlVillage.DataValueField = "id";
            ddlVillage.DataBind();
            ddlMonth.DataSource = Util.GetMonth();
            ddlMonth.DataTextField = "text";
            ddlMonth.DataValueField = "value";
            ddlMonth.DataBind();
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, @"SELECT *  FROM [SSick] where id=" + RecordID);
            if (dt.Rows.Count > 0)
            {
                LinkButton lbtnSave = Page.Master.FindControl("ToolBarContentPlaceHolder").FindControl("lbtnSave") as LinkButton;

                lbtnSave.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                Page.Title = "扶贫帮困资金编辑";
                DataRow dr = dt.Rows[0];
                tbYear.Text = dr["Year"].ToString();
                tbName.Text = dr["Name"].ToString();
              tbAge.Text=dr["age"].ToString();
              tbDistrict.Text = dr["districtMoney"].ToString();
              tbTown.Text = dr["townMoney"].ToString();
                tbDept.Text = dr["dept"].ToString();
                tbSickName.Text = dr["SickName"].ToString();
                tbAppliedMoney.Text = dr["AppliedMoney"].ToString();

                if (dr["IsTownFamliy"].ToString() == "是")
                    rblFamily.Items[0].Selected = true;
                if (dr["IsVillageFamliy"].ToString() == "是")
                    rblFamily.Items[1].Selected = true;
                if (dr["IsTownMedical"].ToString() == "是")
                    rblYiBao.Items[0].Selected = true;
                if (dr["IsVillageMedical"].ToString() == "是")
                    rblYiBao.Items[1].Selected = true;

                ListItem li = ddlVillage.Items.FindByText(dr["BelongTo"].ToString());
                if (li != null)
                    ddlVillage.SelectedValue = li.Value;
                li = ddlMonth.Items.FindByValue(dr["month"].ToString());
                if (li != null)
                    ddlMonth.SelectedValue = li.Value;
               
                li = ddlGendar.Items.FindByText(dr["gender"].ToString());
                if (li != null)
                    ddlGendar.SelectedValue = li.Value;
            }
        }
    }
}